
#ifndef _COMMON_H_
#define _COMMON_H_

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#if HAVE_STDIO_H
#include <stdio.h>
#endif
#if HAVE_CTYPE_H
#include <ctype.h>
#endif
#if HAVE_STDLIB_H
#include <stdlib.h>
#endif
#if HAVE_SYS_TYPES_H
#include <sys/types.h>
#endif
#include <sys/stat.h>
#include <limits.h>
#include <stdint.h>
#include <inttypes.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <assert.h>
#include <getopt.h>
#include <time.h>
#include <netinet/in.h>
#if HAVE_SYS_TIME_H
#include <sys/time.h>
#endif
#if HAVE_SYS_WAIT_H
#include <sys/wait.h>
#endif
#include <signal.h>
#include <fcntl.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <netinet/tcp.h>
#if HAVE_SYS_RESOURCE_H
#include <sys/resource.h>
#endif
#if HAVE_SYS_SOCKET_H
#include <sys/socket.h>
#endif
#if HAVE_STRINGS_H
#include <strings.h>
#endif
#if HAVE_STRING_H
#include <string.h>
#endif
#if HAVE_SYS_MMAN_H
#include <sys/mman.h>
#endif
#if HAVE_SYS_SENDFILE_H
#include <sys/sendfile.h>
#endif
#if HAVE_SYS_UIO_H
#include <sys/uio.h>
#endif
#if HAVE_STDDEF_H
#include <stddef.h>
#endif
#if HAVE_SYS_EVENT_H
#include <sys/event.h>
#endif
#include <stdarg.h>

/* openssl support */
#include <openssl/md5.h>

/* pthread support */
#include <pthread.h>

#include "list.h"
#include "array.h"
#include "buffer.h"
#include "log.h"
#include "mempool.h"
#include "md5.h"
#include "queue.h"
#include "conf.h"
#include "thread.h"
#include "globals.h"
#include "sock_event.h"

typedef void WPF(int, int, int, void *arg);
typedef void PF(int, short, void *);

/* default value of conf */
#define DEFAULT_ACCESS_LOG "/var/log/dolphin.access.log"
#define DEFAULT_DEBUG_LOG "/var/log/dolphin.debug.log"
#define DEFAULT_ERROR_LOG "/var/log/dolphin.error.log"
#define DEFAULT_WARN_LOG "/var/log/dolphin.warn.log"

#define DEFAULE_CACHE_DOWNLOAD_WAITING 1
#define DEFAULT_PORT 80
#define DEFAULT_BIND_ADDR4 "0.0.0.0"
#define DEFAULT_DEBUG_MODE 0
#define DEFAULT_DEBUG_LEVEL 1
#define DEFAULT_WORKER_NUMBER 4
#define DEFAULT_MAX_SIZE_IN_MEM (1<<20)
#define DEFAULT_MAX_SIZE_IN_SHM (1<<20)
#define DEFAULT_OBJECTS 12582910LL
#define DEFAULT_MAX_OBJECT_SIZE_IN_MEM 8192LL
#define DEFAULT_MAXFDS 204800

#define DEFAULT_CLIENT_LIFE (24*60*60)
#define DEFAULT_SERVER_LIFE (24*60*60)
#define DEFAULT_REQUEST_TIMEOUT (60)
#define DEFAULT_REPLY_TIMEOUT (60)
#define DEFAULT_CONNECT_TIMEOUT (60)

#define DIGEST_LENGTH 16
#define MAX_HEADER 4096
#define MAX_BUFFER 4096
#define MAX_URL 4096
#define PAGE_SIZE 4096
#define DEFAULT_MAP_SIZE (4096 * 4)

/* compute array number */
#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
#define NORMAL_SIGNALS {SIGUSR1, SIGUSR2, SIGPIPE, SIGTERM, SIGHUP, SIGINT}
#define COREDUMP_SIGNALS {SIGABRT, SIGFPE, SIGILL, SIGQUIT, SIGSEGV, \
	SIGTRAP, SIGSYS, SIGBUS, SIGXCPU, SIGXFSZ}
#define XMIN(x,y) ((x)<(y)? (x) : (y))
#define XMAX(a,b) ((a)>(b)? (a) : (b))
#define current_time_get(tv) gettimeofday(tv, NULL)
#define timeval_compare(res, t1, t2) \
			{ \
					if(t1.tv_sec > t2.tv_sec) res = 1; \
					else if(t1.tv_sec < t2.tv_sec) res = -1; \
					else res = (t1.tv_usec >= t2.tv_usec ? 1 : -1); \
			}
#define timeval_is_zero(res, t) \
			{ \
					if(t.tv_sec == 0 && t.tv_usec == 0)res=1; \
					else res=0; \
			}
#define safe_free(x) if(x){free(x);x=NULL;}
#endif
